/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package librarystudy.data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

/**
 *
 * @author jmhammond
 */
public class Book {
    public String title;
    public String authorLast;
    public String authorFirst;
    public String shortDescription;
    public String longDescription;

    public boolean hasBeenRead;

    private long publishDate;
    private ArrayList<String> tags;


    // TODO add a better constructor!
    public Book() {

    }

    public void setTitle(String t) {
        title = t;
    }

    public String getTitle() {
        return title;
    }

    public void addTag(String newTag) {
        getTags().add(newTag);
    }
    
    public void addTagsFromSpaceDelimitedString(String tagString) {
        // Assume that this string contains all previous, and desired, tags
        tags = new ArrayList<String>();
        String[] myTags = tagString.split("\\s"); // split on whitespace
        
        for (String tag : myTags) {
            tags.add(tag);
        }
    }

    // Ensure that tags is instantiated before accessing it
    public ArrayList getTags() {
        if (tags == null) {
            tags = new ArrayList<String>();
        }

        return tags;
    }

    public String getTagsAsSpaceDelimitedString() {
        ArrayList myTags = getTags();
        String returnString = "";

        Iterator i = myTags.iterator();
        while (i.hasNext()) {
            returnString += (" " + (String)i.next());
        }

        return returnString.trim();
    }

    public void setPublicationDate(Date d) {
        publishDate = d.getTime();
    }

    public void setPublicationDate(long d) {
        publishDate = d;
    }

    public String getPublicationDateAsPrettyString() {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(publishDate);

        DateFormat format = new SimpleDateFormat("yyyy-MMM-dd");
        return format.format(cal.getTime());
    }

    public void setAuthorFirstName(String name) {
        authorFirst = name;
    }
    
    public void setAuthorLastName(String name) {
        authorLast = name;
    }
    
    public void setAuthorName(String name) {
        String[] nameComponents = name.split("\\,");
        try {
            if (nameComponents != null && nameComponents.length > 0) {
                authorLast = nameComponents[0].trim();
                authorFirst = nameComponents[1].trim();
            }
        } catch (ArrayIndexOutOfBoundsException ex) {
            nameComponents = name.split("\\ ");
            try {
                if (nameComponents != null && nameComponents.length > 0) {
                    authorFirst = nameComponents[0].trim();
                    authorLast = nameComponents[1].trim();
                    // TODO handle Jean Du Prau
                }
            } catch (ArrayIndexOutOfBoundsException ex2) {
                authorLast = name;
            }
        }
    }

    public String getAuthorFirstName() {
        return authorFirst;
    }

    public String getAuthorLastName() {
        return authorLast;
    }

    public String getAuthorFormattedName() {
        // Check some preference about first/last name first
        // if (firstNameFirst) ... {

        return authorLast + ", " + authorFirst;
    }

    public void setDescription(String desc) {
        longDescription = desc;
    }

    public String getDescString() {
        return longDescription;
    }


    @Override
    public String toString() {
        return getAuthorFormattedName() + " " + getTitle() + " " + getPublicationDateAsPrettyString() + getTags();
    }

    public boolean equals(Book b) {
        return title.equalsIgnoreCase(b.getTitle()) &&
                authorLast.equalsIgnoreCase(b.getAuthorLastName()) &&
                authorFirst.equalsIgnoreCase(b.getAuthorFirstName()) &&
                getDescString().equalsIgnoreCase(b.getDescString()) &&
                hasBeenRead == b.hasBeenRead &&
                publishDate == b.publishDate &&
                tags.equals(b.tags);
    }
}
